import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
// state === data
// 改变state 显示的去 commit('mutation-xxx')

// getters === computed
export default new Vuex.Store({
  state: {
    count: 0,
    list: [
      { id: 123, text: "hahah" },
      { id: 1213, text: "h213ah" },
      { id: 12123, text: "ha234ah" },
      { id: 1223, text: "asdhahah" }
    ]
  },
  getters: {
    gt1000(state) {
      return state.list.filter(item => item.id < 1000);
    }
  },
  mutations: {
    // increment
    INCREMENT(state) {
      state.count += 1;
    },
    CREATE_ITEM(state, payload) {
      // console.log(payload.data)
      // state.list.unshift(payload.data)
      state.list.unshift(payload);
      // state.list.push({
      //   id: Number.parseInt(Math.random() * 1000),
      //   text: Date.now()
      // })
    }
  },
  actions: {
    increment(context) {
      // console.log()
      // console.log('hhh')
      context.commit("INCREMENT");
    },
    creatItem(context, data) {
      const { commit } = context;
      // console.log(data)
      commit("CREATE_ITEM", data);
    }
  },
  modules: {}
});
